#include "BattleArena.h"


bool BattleArena::Load(const char * filename)
{
	geo = 0;

	
	


	string str(filename);
	
	string heightmapPath(str);
	

	if(!heightmap.Load(heightmapPath))
		return false;

	heightmap.setSampleSize(100, 100, 100);

	cout << "Map size is " << heightmap.getMapSizeX() << " by " << heightmap.getMapSizeZ() << endl; 

	vector3_t camera_pos;
	camera_pos.x = heightmap.getMapSizeX() / 2.0f + 150;
	camera_pos.z =  heightmap.getMapSizeZ() / 2.0f;
	camera_pos.y =  heightmap.getSampledHeight(camera_pos.x, camera_pos.z) + 50;

	
	cout << camera_pos.x << " " << camera_pos.y  << " " << camera_pos.z << endl;


	camera.setPosition(camera_pos.x, camera_pos.y, camera_pos.z);
	
	

	

	geo = 0; // new wgGeoMipmap(&heightmap);



	static float light0Dir[] = {-500, 2000, 700, 1};
	float ambient[] = { .5f, .5f, .5f, 1.0f};
	float diffuse[] = { 1.0f, .8f, .8f, 1};
	glLightfv(GL_LIGHT0, GL_POSITION, light0Dir);
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);


	shader.Create();
	if(!shader.AddVertexShader("shaders/standard.vert"))
		return false;
	if(!shader.AddFragmentShader("shaders/standard.frag"))
		return false;
	if(!shader.Link())
		return false;



	return true;
}

void BattleArena::Render() 
{

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	vector3_t& cam_pos = camera.getPosition();
	

	camera.UpdateViewMatrix();
	

	glEnable(GL_DEPTH_TEST);


	

	heightmap.Update(camera);
	heightmap.Render();

	
}

BattleArena::~BattleArena()
{
	delete geo;
}